import time
from numpy import *
from solox.public.apm import AppPerformanceMonitor
from solox.public.common import Devices
from common.handle_pid import DHDevices
from common.handle_excel import HandleExcel
from copy_model.copy_excel import create_excel
import pandas as pd
from settings import *

# 配置
# title = "测试场景名称"
# deviceId_001 = "00000000d5328bbf"
# deviceId_002 = "8B1X11V83"
# SDKServer_pkgName = "com.arcvideo.totalmedia.sdkserver"
# CPDemo_pkgName = "com.arcvideo.totalmedia.cpdemo"
# UIDemo_pkgName = "com.arcvideo.totalmedia.uidemo"
# Secondary_device = False

dhd = DHDevices()

CPDemo_processListdic = dhd.getPidNew(deviceId=deviceId_001, pkgName=CPDemo_pkgName)
for each in CPDemo_processListdic:
    if "u10" in each["USER"]:
        pid_u10_cpdemo = each["PID"]
        pid_u10_cpdemo_user = each["USER"]
    else:
        pid_u0_cpdemo = each["PID"]
        pid_u0_cpdemo_user = each["USER"]
UIDemo_processListdic = dhd.getPidNew(deviceId=deviceId_001, pkgName=UIDemo_pkgName)
for each in UIDemo_processListdic:
    if "u10" in each["USER"]:
        pid_u10_uidemo = each["PID"]
        pid_u10_uidemo_user = each["USER"]
    else:
        pid_u0_uidemo = each["PID"]
        pid_u0_uidemo_user = each["USER"]
SDKServer_processListdic = dhd.getPidNew(deviceId=deviceId_001, pkgName=SDKServer_pkgName)
for each in SDKServer_processListdic:
    if each["pkgName"] == "com.arcvideo.totalmedia.sdkserver:arc_center_service":
        pid_server_001 = each["PID"]
apm_pid_pid_u0_cpdemo = AppPerformanceMonitor(pkgName=CPDemo_pkgName, platform='Android', deviceId=deviceId_001,
                                              surfaceview=True,
                                              noLog=False, pid=pid_u0_cpdemo, record=False, collect_all=False)
apm_pid_pid_u0_uidemo = AppPerformanceMonitor(pkgName=UIDemo_pkgName, platform='Android', deviceId=deviceId_001,
                                              surfaceview=True,
                                              noLog=False, pid=pid_u0_uidemo, record=False, collect_all=False)
apm_pid_pid_u10_cpdemo = AppPerformanceMonitor(pkgName=CPDemo_pkgName, platform='Android', deviceId=deviceId_001,
                                               surfaceview=True,
                                               noLog=False, pid=pid_u10_cpdemo, record=False, collect_all=False)
apm_pid_pid_u10_uidemo = AppPerformanceMonitor(pkgName=UIDemo_pkgName, platform='Android', deviceId=deviceId_001,
                                               surfaceview=True,
                                               noLog=False, pid=pid_u10_uidemo, record=False, collect_all=False)
apm_pid_pid_server_001 = AppPerformanceMonitor(pkgName=SDKServer_pkgName, platform='Android', deviceId=deviceId_001,
                                               surfaceview=True,
                                               noLog=False, pid=pid_server_001, record=False, collect_all=False)
apm_instances_001 = [apm_pid_pid_u0_cpdemo, apm_pid_pid_u0_uidemo, apm_pid_pid_u10_cpdemo, apm_pid_pid_u10_uidemo,
                     apm_pid_pid_server_001]
if Secondary_device:
    CPDemo_processListdic_002 = dhd.getPidNew(deviceId=deviceId_002, pkgName=CPDemo_pkgName)
    for each in CPDemo_processListdic_002:
        pid_u0_cpdemo_002 = each["PID"]
        pid_u0_cpdemo_user_002 = each["USER"]
    UIDemo_processListdic_002 = dhd.getPidNew(deviceId=deviceId_002, pkgName=UIDemo_pkgName)
    for each in UIDemo_processListdic_002:
        pid_u0_uidemo_002 = each["PID"]
        pid_u0_uidemo_user_002 = each["USER"]
    SDKServer_processListdic_002 = dhd.getPidNew(deviceId=deviceId_002, pkgName=SDKServer_pkgName)
    for each in SDKServer_processListdic_002:
        if each["pkgName"] == "com.arcvideo.totalmedia.sdkserver:arc_center_service":
            pid_server_002 = each["PID"]
    apm_pid_pid_u0_cpdemo_002 = AppPerformanceMonitor(pkgName=CPDemo_pkgName, platform='Android', deviceId=deviceId_002,
                                                      surfaceview=True,
                                                      noLog=False, pid=pid_u0_cpdemo_002, record=False,
                                                      collect_all=False)
    apm_pid_pid_u0_uidemo_002 = AppPerformanceMonitor(pkgName=UIDemo_pkgName, platform='Android', deviceId=deviceId_002,
                                                      surfaceview=True,
                                                      noLog=False, pid=pid_u0_uidemo_002, record=False,
                                                      collect_all=False)
    apm_pid_pid_server_002 = AppPerformanceMonitor(pkgName=SDKServer_pkgName, platform='Android', deviceId=deviceId_002,
                                                   surfaceview=True,
                                                   noLog=False, pid=pid_server_002, record=False, collect_all=False)
    apm_instances_002 = [apm_pid_pid_u0_cpdemo_002, apm_pid_pid_u0_uidemo_002, apm_pid_pid_server_002]


def get_perf_data(apm_instances):
    cpu_list = []
    mem_list = []
    for apm_instance in apm_instances:
        cpu_list.append(apm_instance.collectCpu()['appCpuRate'] * 8)
        mem_list.append(apm_instance.collectMemory()['total'])
    cpu_list.append(round(sum(cpu_list), 2))
    mem_list.append(round(sum(mem_list), 2))
    return cpu_list, mem_list


def start_test_1(delay, num):
    filename = create_excel("per_datas_1.xlsx")
    do_excel = HandleExcel("性能监控", filename=filename)
    do_excel.write_data(row=1, column=1, value=title)

    row = 4
    time.sleep(1)
    cpu_all_data_list = []
    mem_all_data_list = []
    while num > 0:
        cpu_list, mem_list = get_perf_data(apm_instances_001)
        cpu_all_data_list.append(cpu_list)
        mem_all_data_list.append(mem_list)
        for i, val in enumerate(cpu_list):
            do_excel.write_data(row=row, column=i + 2, value=val)

        for i, val in enumerate(mem_list):
            do_excel.write_data(row=row, column=i + 10, value=val)
        row += 1
        num -= 1
        time.sleep(delay)
    df_cpu = pd.DataFrame(cpu_all_data_list)
    cpu_means_list = [round(each, 2) for each in list(df_cpu.mean())]
    cpu_max_values_list = [round(each, 2) for each in list(df_cpu.max())]
    cpu_min_values_list = [round(each, 2) for each in list(df_cpu.min())]
    df_mem = pd.DataFrame(mem_all_data_list)
    mem_means_list = [round(each, 2) for each in list(df_mem.mean())]
    mem_max_values_list = [round(each, 2) for each in list(df_mem.max())]
    mem_min_values_list = [round(each, 2) for each in list(df_mem.min())]
    row += 1
    do_excel.write_data(row=row, column=1, value="平均值")
    for i, val in enumerate(cpu_means_list):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_means_list):
        do_excel.write_data(row=row, column=i + 10, value=val)
    row += 1
    do_excel.write_data(row=row, column=1, value="最大值")
    for i, val in enumerate(cpu_max_values_list):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_max_values_list):
        do_excel.write_data(row=row, column=i + 10, value=val)
    row += 1
    do_excel.write_data(row=row, column=1, value="最小值")
    for i, val in enumerate(cpu_min_values_list):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_min_values_list):
        do_excel.write_data(row=row, column=i + 10, value=val)
    do_excel.close()


def start_test_2(delay, num):
    filename = create_excel("per_datas_2.xlsx")
    do_excel = HandleExcel("性能监控", filename=filename)
    row = 4
    time.sleep(1)
    cpu_all_data_list_001 = []
    mem_all_data_list_001 = []
    cpu_all_data_list_002 = []
    mem_all_data_list_002 = []
    while num > 0:
        cpu_list_001, mem_list_001 = get_perf_data(apm_instances_001)
        cpu_all_data_list_001.append(cpu_list_001)
        mem_all_data_list_001.append(mem_list_001)
        cpu_list_002, mem_list_002 = get_perf_data(apm_instances_002)
        cpu_all_data_list_002.append(cpu_list_002)
        mem_all_data_list_002.append(mem_list_002)
        for i, val in enumerate(cpu_list_001):
            do_excel.write_data(row=row, column=i + 2, value=val)

        for i, val in enumerate(mem_list_001):
            do_excel.write_data(row=row, column=i + 10, value=val)
        for i, val in enumerate(cpu_list_002):
            do_excel.write_data(row=row, column=i + 18, value=val)
        for i, val in enumerate(mem_list_002):
            do_excel.write_data(row=row, column=i + 24, value=val)
        row += 1
        num -= 1
        time.sleep(delay)
    df_cpu_001 = pd.DataFrame(cpu_all_data_list_001)
    cpu_means_list_001 = [round(each, 2) for each in list(df_cpu_001.mean())]
    cpu_max_values_list_001 = [round(each, 2) for each in list(df_cpu_001.max())]
    cpu_min_values_list_001 = [round(each, 2) for each in list(df_cpu_001.min())]
    df_mem_001 = pd.DataFrame(mem_all_data_list_001)
    mem_means_list_001 = [round(each, 2) for each in list(df_mem_001.mean())]
    mem_max_values_list_001 = [round(each, 2) for each in list(df_mem_001.max())]
    mem_min_values_list_001 = [round(each, 2) for each in list(df_mem_001.min())]
    df_cpu_002 = pd.DataFrame(cpu_all_data_list_002)
    cpu_means_list_002 = [round(each, 2) for each in list(df_cpu_002.mean())]
    cpu_max_values_list_002 = [round(each, 2) for each in list(df_cpu_002.max())]
    cpu_min_values_list_002 = [round(each, 2) for each in list(df_cpu_002.min())]
    df_mem_002 = pd.DataFrame(mem_all_data_list_002)
    mem_means_list_002 = [round(each, 2) for each in list(df_mem_002.mean())]
    mem_max_values_list_002 = [round(each, 2) for each in list(df_mem_002.max())]
    mem_min_values_list_002 = [round(each, 2) for each in list(df_mem_002.min())]
    row += 1
    do_excel.write_data(row=row, column=1, value="平均值")
    for i, val in enumerate(cpu_means_list_001):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_means_list_001):
        do_excel.write_data(row=row, column=i + 10, value=val)
    for i, val in enumerate(cpu_means_list_002):
        do_excel.write_data(row=row, column=i + 18, value=val)
    for i, val in enumerate(mem_means_list_002):
        do_excel.write_data(row=row, column=i + 24, value=val)
    row += 1
    do_excel.write_data(row=row, column=1, value="最大值")
    for i, val in enumerate(cpu_max_values_list_001):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_max_values_list_001):
        do_excel.write_data(row=row, column=i + 10, value=val)
    for i, val in enumerate(cpu_max_values_list_002):
        do_excel.write_data(row=row, column=i + 18, value=val)
    for i, val in enumerate(mem_max_values_list_002):
        do_excel.write_data(row=row, column=i + 24, value=val)
    row += 1
    do_excel.write_data(row=row, column=1, value="最小值")
    for i, val in enumerate(cpu_min_values_list_001):
        do_excel.write_data(row=row, column=i + 2, value=val)
    for i, val in enumerate(mem_min_values_list_001):
        do_excel.write_data(row=row, column=i + 10, value=val)
    for i, val in enumerate(cpu_min_values_list_002):
        do_excel.write_data(row=row, column=i + 18, value=val)
    for i, val in enumerate(mem_min_values_list_002):
        do_excel.write_data(row=row, column=i + 24, value=val)
    do_excel.close()


if __name__ == '__main__':
    if Secondary_device:
        start_test_2(0.2, 36)
    else:
        start_test_1(0.2, 36)
